package com.example.yamba;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class StatusProvider extends ContentProvider {
    private static final String TAG = StatusProvider.class.getSimpleName();
    public static final Uri CONTENT_URi =
            Uri.parse("content://com.example.yamba/statusprovider");
    public static final String SINGLE_RECORD_MIME_TYPE =
            "vnd.android.cursor.item/vnd.example.yamba.status";
    public static final String MULTIPLE_RECORDS_MIME_TYPE =
            "vnd.android.cursor.dir/vnd.example.yamba.mstatus";
    private StatusData statusData ;
    public StatusProvider() {
    }
    public boolean onCreate(){
        Context context = getContext();
        YambaApplication yamba = (YambaApplication)context.getApplicationContext();
        statusData = yamba.getStatusData();
        return true;
    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        long id = this.getId(uri);
        SQLiteDatabase db = statusData.getDbhelper().getWritableDatabase();
        try {
            if (id < 0) {
                return db.delete(StatusData.TABLE, selection, selectionArgs);
            } else {
                return db.delete(StatusData.TABLE, StatusData.C_ID + "=" + id, null);
            }
        } finally {
            db.close();
        }
    }

    @Override
    public String getType(Uri uri) {
        long id = getId(uri);
        if(id<0)
            return this.MULTIPLE_RECORDS_MIME_TYPE;
        else
            return this.SINGLE_RECORD_MIME_TYPE;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long id = statusData.insertOrIgnore(values);
        if(id == -1 )
            throw new RuntimeException();
        else
            return ContentUris.withAppendedId(uri,id);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = statusData.getDbhelper().getReadableDatabase();
        long id = getId(uri);
        if(id<0){
            return db.query(statusData.TABLE,projection,selection,selectionArgs,null,null,sortOrder);
        }else{
            return db.query(statusData.TABLE,projection,statusData.C_ID+"="+id,null,null,null,sortOrder);
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        SQLiteDatabase db = statusData.getDbhelper().getWritableDatabase();
        long id = this.getId(uri);
        try{
            if(id <0 ){
                return db.update(statusData.TABLE,values,selection,selectionArgs);
            }else{
                return db.update(statusData.TABLE,values,statusData.C_ID+"="+id,null);
            }
        }finally{
            db.close();
        }
    }
    private long getId(Uri uri){
        String lastPath = uri.getLastPathSegment();
        if(lastPath!=null ){
            try{
                return Long.valueOf(lastPath);
            }catch(NumberFormatException e){
                e.printStackTrace();
            }
        }
        return -1;
    }
}
